package com.chance.cc.crawler.development.command.job.domain.zhaoshang;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequest;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.filter.FilterInfo;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2020/12/25 12:46
 * @Description
 *      招商 中标速递
 **/
public class ZhaoshangBiddingReprotCrawlerScheduleJob {

    public static String domainId = "zhaoshang";
    private static final String site = "biddingReprot";
    private static final String Cookie = "zsid=8173fc36-130d-4c29-bb1e-0a46bd76b55e";
    public static String list = "http://zhaoshang.zkdj.com/folder/list";
    private static CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand("192.168.1.215",9599);


    public static void main(String[] args) {
        publishCrawlerScheduleJobInfo();
    }

    public static CrawlerJob publishCrawlerScheduleJobInfo(){


        CrawlerJob peopleCrawlerSchdule = peopleCrawlerSchdule(domainId);

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domainId);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(peopleCrawlerSchdule));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("招商中标速递单次采集");
        crawlerScheduleJob.setCrawlerKey(peopleCrawlerSchdule.generateCrawlerKey());
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布crawler文章作业：" + page.getRawText());
        return peopleCrawlerSchdule;

    }

    public static CrawlerJob peopleCrawlerSchdule(String domain) {
        return CrawlerJob.builder()
                .triggerInfo(
                        domainId,
                        CrawlerMetaConstant.ScheduleJobTrigger_Simple,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site,ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domainId,"queue")))
                .fileResultPipeline(site + "List", "/data/chance_crawler_test/data/zhaoshang/biddingReprot/zhaoshang_"+site + "List.log", false)
                .fileResultPipeline(site + "Seach", "/data/chance_crawler_test/data/zhaoshang/biddingReprot/zhaoshang_"+site + "Seach.log", false)
                .fileResultPipeline(site + "EnterpriseInfo", "/data/chance_crawler_test/data/zhaoshang/biddingReprot/zhaoshang_"+site+"EnterpriseInfo.log", false)
                .requestRecord(listRecord(domain,list,site))
//                .crawlerJobThreadNumber(10)
                .build();
    }

    public static CrawlerRequestRecord listRecord(String domain,String url,String site){
        Proxy proxy = new Proxy();
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("HL89Q19E86E2987D");
        proxy.setPassword("71F33D94CE5F7BF2");

        FilterInfo countFilter = new FilterInfo();
        countFilter.setCountTotalNum(5);
        countFilter.setCurCount(1);
        countFilter.setFilter(CrawlerEnum.CrawlerRecordFilter.count);

        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPageItem)
                .domain(domain)
                .httpUrl(url)
                .recordKey(url)
                .httpConfig(HttpConfig.me(domain))
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .filter(CrawlerEnum.CrawlerRecordFilter.key)
                .turnPageFilterInfo(countFilter)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(domain))
                .releaseTime(System.currentTimeMillis())
                .build();


        HttpRequest httpRequest = crawlerRequestRecord.getHttpRequest();
        httpRequest.setMethod(HttpConstant.Method.POST);
        httpRequest.addHeader("Accept"," application/json, text/javascript, */*; q=0.01");
        httpRequest.addHeader("Cookie",Cookie);
        httpRequest.addHeader("Host"," zhaoshang.zkdj.com");
        httpRequest.addHeader("Origin"," http://zhaoshang.zkdj.com");
        httpRequest.addHeader("Referer"," http://zhaoshang.zkdj.com/"+site);
        httpRequest.addHeader("User-Agent"," Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36");

        crawlerRequestRecord.tagsCreator().bizTags().addSite(site);

        return crawlerRequestRecord;
    }
}
